package com.medical.blood.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.blood.entity.BloodReservation;
import com.medical.blood.mapper.BloodReservationMapper;
import com.medical.blood.service.imp.BloodReservationService;
import org.springframework.stereotype.Service;

@Service
public class BloodReservationServiceImpl extends ServiceImpl<BloodReservationMapper, BloodReservation> implements BloodReservationService {
    @Override
    public boolean saveBloodReservation(BloodReservation bloodReservation) {
        if (checkReservationConflict(bloodReservation)) {
            return false;
        }
        return save(bloodReservation);
    }

    @Override
    public java.util.List<BloodReservation> listBloodReservationByUserId(Long userId) {
        return lambdaQuery().eq(BloodReservation::getUserId, userId).list();
    }

    @Override
    public boolean checkReservationConflict(BloodReservation bloodReservation) {
        LambdaQueryWrapper<BloodReservation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BloodReservation::getDeviceId, bloodReservation.getDeviceId())
                .eq(BloodReservation::getStatus, "未采血")
                .eq(BloodReservation::getReservationTime, bloodReservation.getReservationTime());
        return count(wrapper) > 0;
    }
}